package com.kdyzm.demo.springboot.utils;

import javassist.*;
import lombok.extern.slf4j.Slf4j;

/**
 * @author kdyzm
 * @date 2022/3/4
 */
@Slf4j
public class JavassitUtil {

    public static void init(){
        try {
            log.info("修改默认行为");
            ClassPool classPool = ClassPool.getDefault();
            String clsName = "org.apache.ibatis.executor.resultset.DefaultResultSetHandler";
            CtClass ctClass = classPool.get(clsName);
            ctClass.defrost();
            CtMethod ctMethod = ctClass.getDeclaredMethod("getRowValue", new CtClass[]{
                    classPool.get("org.apache.ibatis.executor.resultset.ResultSetWrapper"),
                    classPool.get("org.apache.ibatis.mapping.ResultMap"),
                    classPool.get("java.lang.String")
                }
            );
            classPool.importPackage("com.kdyzm.demo.springboot.config.CustomResultHandler");
            classPool.importPackage("org.apache.ibatis.executor.resultset");
            classPool.importPackage("org.apache.ibatis.executor.loader");
            classPool.importPackage("org.apache.ibatis.reflection");
            classPool.importPackage("com.kdyzm.demo.springboot.config.plugin");
            ctMethod.setBody("{" +
                "final ResultLoaderMap lazyLoader = new ResultLoaderMap();\n" +
                "        Object rowValue = createResultObject($1, $2, lazyLoader, $3);\n" +
                "        if (rowValue != null && !hasTypeHandlerForResultObject($1, $2.getType())) {\n" +
                "            final MetaObject metaObject = configuration.newMetaObject(rowValue);\n" +
                "            boolean foundValues = this.useConstructorMappings;\n" +
                "            if (shouldApplyAutomaticMappings($2, false)) {\n" +
                "                foundValues = applyAutomaticMappings($1, $2, metaObject, $3) || foundValues;\n" +
                "            }\n" +
                "            foundValues = applyPropertyMappings($1, $2, metaObject, lazyLoader, $3) || foundValues;\n" +
                "            foundValues = lazyLoader.size() > 0 || foundValues;\n" +
                "            rowValue = foundValues || configuration.isReturnInstanceForEmptyRow() ? rowValue : null;\n" +
                "        }\n" +
                "        if(rowValue!=null){\n" +
                "            ResultExtWrapper resultExtWrapper = new ResultExtWrapper(\n" +
                "                configuration,\n" +
                "                $1,\n" +
                "                $2,\n" +
                "                rowValue\n" +
                "            );\n" +
                "            return resultExtWrapper.extFillData();\n" +
                "        }"+
                "        return rowValue;" +
                "}");
            // 转为class
            ctClass.toClass();
//            // 释放对象
            ctClass.detach();
        } catch (NotFoundException | CannotCompileException e) {
            log.error("", e);
        }
    }
}
